using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace LFromeWork
{
    public enum UiResourceType
    {
        //ui主窗口
        EUiWindow,

        //ui子窗口
        EUiPanel,
    }

    public class UiResourcesInfo
    {
        public UiResourceType eType = UiResourceType.EUiWindow;
        public string StrResourcePath { get; set; }
        public Object ResObj { get; set; }
    }

    public class UICtrlBase
    {
        private bool m_Showing = false;
        protected UIWindow m_UIWindow = null;
        protected GameObject m_UIWindowGo = null;
        protected Dictionary<string,UiResourcesInfo> m_NeedResourcesDic = new Dictionary<string,UiResourcesInfo>();
        //主窗口
        protected UiResourcesInfo m_MainWindowInfo = null;
        //资源加载完成数量
        protected int ResourcesCompleteCount = 0;

        private int m_WindowOrder;

        public int WindowOrder
        {
            get { return m_WindowOrder; }
            set
            {
                m_WindowOrder = value;
                if (m_UIWindow != null)
                {
                    m_UIWindow.SetCanvasOrder(m_WindowOrder);
                }
            }
        }

        public virtual void Open()
        {
            if (m_Showing)
                return;

            m_Showing = true;

            GameLaunch.UI.AddUI(this);
        }

        public void DoOpen()
        {
            foreach (KeyValuePair<string,UiResourcesInfo> item in m_NeedResourcesDic)
            {
                if (item.Value.ResObj != null)
                {
                    ResourcesCompleteCount++;
                    continue;
                }
                GameLaunch.Resources.LoadAssetAsync(item.Value.StrResourcePath,HandleUIResourceOK);
            }
        }

        public virtual void Close()
        {
            if (!m_Showing)
            {
                return;
            }

            m_Showing = false;
            GameLaunch.UI.RemoveUI(this);
            
            if (m_UIWindow != null)
            {
                m_UIWindow.Release();
                m_UIWindow = null;
            }
            
            if (m_UIWindowGo != null)
            {
                GameLaunch.Pool.GoPool.Despawn(m_UIWindowGo);
                m_UIWindowGo = null;
            }
            
            foreach (KeyValuePair<string,UiResourcesInfo> item in m_NeedResourcesDic)
            {
                if (item.Value.ResObj != null)
                    GameLaunch.Resources.ReleaseAsset(item.Value.StrResourcePath,HandleUIResourceOK);
            }

            m_NeedResourcesDic.Clear();
            ResourcesCompleteCount = 0;
            m_MainWindowInfo = null;
        }

        /// <summary>
        /// 打开界面前需要加载的资源
        /// </summary>
        /// <param name="strPath"></param>
        /// <param name="eType"></param>
        protected void AddNeedResources(string strPath, UiResourceType eType)
        {
            UiResourcesInfo info;
            if(m_NeedResourcesDic.TryGetValue(strPath,out info))
                return;

            info = new UiResourcesInfo();
            info.StrResourcePath = strPath;
            info.eType = eType;
            m_NeedResourcesDic[strPath] = info;
            
            //设置主窗口
            if (eType == UiResourceType.EUiWindow)
            {
                if (m_MainWindowInfo == null)
                    m_MainWindowInfo = info;
                else
                    GameLaunch.Log("主窗口 只允许一个 path:: " + strPath);
            }
        }
        
        /// <summary>
        /// 资源加载完成
        /// </summary>
        /// <param name="strPath"></param>
        /// <param name="objResource"></param>
        private void HandleUIResourceOK(string strPath, UnityEngine.Object objResource)
        {
            if (!m_Showing)
            {
                return;
            }
            
            UiResourcesInfo info;
            if (!m_NeedResourcesDic.TryGetValue(strPath, out info))
            {
                GameLaunch.Log("资源加载完成，但未找到需要加载Info信息");
                return;
            }

            if (objResource != null)
            {
                ResourcesCompleteCount++;
                info.ResObj = objResource;
            }
            else
            {
                Close();
                GameLaunch.Log("加载资源为null path: " + strPath); 
            }
            
            // 每下完事一个就检查一下是否都搞定了 [3/23/2014 环山]
            CheckAllResInfoFinish();
        }
        
        /// <summary>
        /// 检查是否所有资源加载完成
        /// </summary>
        private void CheckAllResInfoFinish()
        {
            if(ResourcesCompleteCount != m_NeedResourcesDic.Count)
                return;
            OnUiLoaded();
        }

        /// <summary>
        /// 真正加载ui 的地方
        /// </summary>
        /// <param name="bLoaded"></param>
        private void OnUiLoaded()
        {
            if(m_UIWindow != null)
                return;
            
            if (m_MainWindowInfo == null)
            {   
                GameLaunch.LogError("load 主ui error: ");
                return;
            }
            
            m_UIWindowGo = GameLaunch.Pool.GoPool.Spawn(m_MainWindowInfo.StrResourcePath, GameLaunch.UI.Canvas.transform);
            RectTransform rectTrans = m_UIWindowGo.GetComponent<RectTransform>();

            m_UIWindowGo.transform.localScale = Vector3.one;
            m_UIWindowGo.transform.localPosition = Vector3.zero;
            // m_UIWindowGo.transform.localEulerAngles = Vector3.zero;
            rectTrans.anchorMin = Vector2.zero;
            rectTrans.anchorMax = Vector2.one;
            rectTrans.offsetMax = Vector2.zero;
            rectTrans.offsetMin = Vector2.zero;
            
            m_UIWindow = m_UIWindowGo.GetComponent<UIWindow>();

            if (m_UIWindow == null)
            {
                GameLaunch.LogError("没有发现UIWindow 组件: " + m_MainWindowInfo.StrResourcePath);
                return;
            }
            
            GameLaunch.UI.RefreshCanvasOrder();
            m_UIWindow.SetUICtrl(this);
            m_UIWindow.Init();
        }
    }
}
